<template>
  <div class="page">

    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="城市：">
          <city-select v-model="formInline.cities" :multiple="false"></city-select>
        </el-form-item>
        <el-form-item label="来源：">
          <user-source v-model="formInline.channelSources"></user-source>
        </el-form-item>
        <el-form-item label="广告代号：">
          <adcode-select v-model="formInline.adSources"></adcode-select>
        </el-form-item>
        <el-form-item label="客户需求：">
          <project-select v-model="formInline.projects"></project-select>
        </el-form-item>
        <el-form-item label="报名时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.term" placeholder="请输入电话/姓名"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList(1)">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button type="primary" size="small" :disabled="!checkList ||checkList.length<=0" @click="openDialog('showSmartAssign')">批量智能分配</el-button>
        <el-button type="default" size="small" :disabled="!checkList ||checkList.length<=0" @click="openDialog('showHandAssign')">批量手动分配</el-button>
        <el-button type="default" size="small" @click="openDialog('showBatchImport')">批量导入线索</el-button>
        <el-button type="default" size="small" @click="openDialog('showEnterClues')">录入线索</el-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" :data="tableData" @selection-change="onSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="departmentName" label="线索信息" width="150">
          <template slot-scope="{row}">
            <tab-user :user="userFormatter(row)" v-if="row" :disabled="true"></tab-user>
          </template>
        </el-table-column>
        <el-table-column prop="dentalProblems" label="牙齿问题" show-overflow-tooltip />
        <el-table-column prop="intentProjects" label="客户需求" show-overflow-tooltip />
        <el-table-column prop="age" label="年龄" show-overflow-tooltip />
        <el-table-column prop="wxAccount" label="微信" show-overflow-tooltip />
        <el-table-column prop="channelSource" label="来源" show-overflow-tooltip />
        <el-table-column prop="adSource" label="广告代号" show-overflow-tooltip />
        <el-table-column prop="enrollTime" label="报名时间" show-overflow-tooltip />
        <el-table-column prop="address" label="操作" width="100" align="center" fixed="right">
          <template slot-scope="{row}">
            <el-link type="primary" class="el_link" :id="row.id" @click="handAssignEvent(row)">手动分配</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

    <!-- 批量智能分配 开始 -->
    <smart-assign v-model="showSmartAssign" v-if="showSmartAssign" :list="checkList" @change="getList(1)"></smart-assign>
    <!-- 批量智能分配 end -->

    <!-- 批量手动分配 开始 -->
    <hand-assign v-model="showHandAssign" v-if="showHandAssign" :list="checkList" @change="getList(1)"></hand-assign>
    <!-- 批量手动分配 end -->

    <!-- 批量导入 开始 -->
    <batch-import v-model="showBatchImport" v-if="showBatchImport" @change="getList(1)"></batch-import>
    <!-- 批量导入 end -->

    <!-- 录入线索 开始 -->
    <enter-clues v-model="showEnterClues" v-if="showEnterClues" @change="getList(1)"></enter-clues>
    <!-- 录入线索 end -->

  </div>
</template>

<script>
import { showModal, showToast, deepClone } from "@/common/util";
import { GET_UNASSIGNED_CLUE_SEARCH } from "@/http/clues";
import SmartAssign from "./smartAssign.vue";
import HandAssign from "./handAssign.vue";
import BatchImport from "./batchImport.vue";
import EnterClues from "./enterClues.vue";
export default {
  components: {
    SmartAssign,
    HandAssign,
    BatchImport,
    EnterClues,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      checkList: [],
      showSmartAssign: false,
      showHandAssign: false,
      showBatchImport: false,
      showEnterClues: false,
      handAssignTitle:''
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 格式化处理用户信息
    userFormatter(row) {
      let o = deepClone(row);
      o.customerName = o.name;
      o.customerPhone = o.phone;
      o.pageType = this.id;
      o.id = row.clueId;
      return o;
    },

    /** 获取用户列表 */
    getList(current) {
      let _this = this;
      let oParams = _this.getParams();
      oParams.pageNumber = current || _this.pageNumber;
      console.log(oParams);
      _this.loading = true;
      GET_UNASSIGNED_CLUE_SEARCH(oParams).then((res) => {
        console.log(res);
        _this.loading = false;
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },
    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let o = JSON.parse(JSON.stringify(this.formInline));
      let m = o.orderTime || [];
      if (m && m.length > 0) {
        o.enrollStartTime = m[0];
        o.enrollEndTime = m[1];
      }

      delete o.orderTime;

      o.pageSize = this.pageSize;
      return o;
    },

    /** 重置 */
    rest() {
      this.formInline = {};
      this.pageNumber = 1;
      this.getList();
    },

    // 监听表格变化
    onSelectionChange(selection) {
      this.checkList = selection;
    },

    // 打开弹框
    openDialog(id, row) {
      this[id] = true;
    },

    // 手动分配
    handAssignEvent(row){
      this.showHandAssign = true;
      this.checkList = [row]
    }

  },
};
</script>

<style lang="scss" scoped>
</style>
